﻿using leetcodeAnswer.Code;
using System;
using System.Collections.Generic;
using System.Text;

namespace leetcodeAnswer.Answer
{
    [Answer(
    Index = 17,
    Name = "电话号码的字母组合",
    Url = "https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/",
    Descript = @"给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
示例:
输入：""23""
输出：[""ad"", ""ae"", ""af"", ""bd"", ""be"", ""bf"", ""cd"", ""ce"", ""cf""].")]
    public class No_017_LetterCombinations
    {
        [AnswerRun]
        public IList<string> LetterCombinations(string digits)
        {
            IList<string> data = new List<string>();

            Dictionary<char, string> dic_setting = new Dictionary<char, string>
            {
                { '2', "abc" },
                { '3', "def" },
                { '4', "ghi" },
                { '5', "jkl" },
                { '6', "mno" },
                { '7', "pqrs" },
                { '8', "tuv" },
                { '9', "wxyz" }
            };

            RecursionData(ref data, digits, 0, "", dic_setting);

            return data;
        }

        private void RecursionData(ref IList<string> arg_data, string arg_digits, int arg_index, string arg_str, Dictionary<char, string> arg_dic_setting)
        {
            if (arg_digits.Length > arg_index && arg_dic_setting.ContainsKey(arg_digits[arg_index]))
            {
                foreach (var item in arg_dic_setting[arg_digits[arg_index]])
                {
                    if (arg_digits.Length == arg_index + 1)
                    {
                        arg_data.Add(string.Format("{0}{1}", arg_str, item));
                    }
                    else
                    {
                        RecursionData(ref arg_data, arg_digits, (arg_index + 1), string.Format("{0}{1}", arg_str, item), arg_dic_setting);
                    }

                }
            }
        }

        /// <summary>
        /// 验证参数
        /// </summary>
        [AnswerParameter]
        public List<object[]> Parameter { get; set; }

        public No_017_LetterCombinations()
        {
            //参数初始化
            Parameter = new List<object[]>() {
                new object[]{ "23" },
                new object[]{ "" },
                new object[]{ "234" },
            };
        }
    }
}
